/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.entities;

import inventory.Inventory;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Random;
import rpg.Game;
import rpg.Location;
import rpg.RPG;
import rpg.gfx.Bar;
import rpg.gfx.Bitmap;
import rpg.util.ImageUtil;

/**
 * rothens.tarhely.biz
 * @author Rothens
 */
public class Player extends LivingEntity {

	public static final int UP = 0;
	public static final int LEFT = 1;
	public static final int DOWN = 2;
	public static final int RIGHT = 3;
	private Bitmap[] playerBM;
	public int energy = 100;
	private Game game;
	public int xOffs;
	public int yOffs;
	public int dir;
	public boolean moving;
	public boolean running;
	public boolean resting;
	public static int animphase;
	public Bar energyBar;
	int animDelay = 5;
	int restDelay = 20;
	int consumeDelay = 5;
	int damageDelay = 0;
	
	private Inventory inventory;

	public Player(Location loc, Game actual) {
		super(150, loc);
		inventory = new Inventory();
		energyBar = new Bar(100, 6, 0xffff00, 0x0, 100, 100);
		playerBM = new Bitmap[12];
		this.game = actual;
		dir = DOWN;

		animphase = 0;
		BufferedImage image = ImageUtil.createImageIcon("/res/character.png");//.getRGB(0, 0, playerBM.width, playerBM.height, playerBM.pixels, 0, playerBM.width);
		for (int i = 0; i < 12; i++) {
			int col = i % 3;
			int row = i / 3;
			playerBM[i] = new Bitmap(64, 64);
			image.getSubimage(col * 64, row * 64, 64, 64).getRGB(0, 0, playerBM[i].width, playerBM[i].height, playerBM[i].pixels, 0, playerBM[i].width);
		}
		Game.eh.addToActive(this);
	}

	private void checkControls() {
		if (RPG.listener.isKeyDown(KeyEvent.VK_ESCAPE)) {
			System.exit(0);
		}
		int moves = 0;
		int tmp = -1;

		running = RPG.listener.isKeyDown(KeyEvent.VK_SHIFT);
		int speed = (running && energy > 0 && !resting) ? 3 : 2;


		if (RPG.listener.isKeyDown(KeyEvent.VK_A) || RPG.listener.isKeyDown(KeyEvent.VK_LEFT)) {
			if (loc.move(Location.LEFT, speed, game.actual)) {
				dir = LEFT;
				moves++;
			} else {
				tmp = LEFT;
			}
		} else if (RPG.listener.isKeyDown(KeyEvent.VK_D) || RPG.listener.isKeyDown(KeyEvent.VK_RIGHT)) {
			if (loc.move(Location.RIGHT, speed, game.actual)) {
				dir = RIGHT;
				moves++;
			} else {
				tmp = RIGHT;
			}
		}
		if (RPG.listener.isKeyDown(KeyEvent.VK_W) || RPG.listener.isKeyDown(KeyEvent.VK_UP)) {
			if (loc.move(Location.UP, speed, game.actual)) {
				dir = UP;
				moves++;
			} else {
				tmp = UP;
			}
		} else if (RPG.listener.isKeyDown(KeyEvent.VK_S) || RPG.listener.isKeyDown(KeyEvent.VK_DOWN)) {
			if (loc.move(Location.DOWN, speed, game.actual)) {
				dir = DOWN;
				moves++;
			} else {
				tmp = DOWN;
			}
		}
		if(RPG.listener.isKeyDown(KeyEvent.VK_SPACE)){
			interact();
		}
		
		if(RPG.listener.isKeyDown(KeyEvent.VK_CONTROL)){
			tryDamage();
		}
		
		moving = moves > 0;
		if (moving && running && energy > 0 && !resting) {
			consumeDelay--;
			if (consumeDelay <= 0) {
				consumeDelay = 5;
				energy--;
				if (energy < 2) {
					resting = true;
				}
			}
		}
		if (!moving && tmp >= 0) {
			dir = tmp;
		}
	}

	private void checkMap() {
		if (loc.getX() < 320) {
			xOffs = 320 - loc.getX();
		} else if (loc.getX() > game.actual.getWidth() * 64 - 320) {
			xOffs = (game.actual.getWidth() * 64 - 320) - loc.getX();
		} else {
			xOffs = 0;
		}

		if (loc.getY() < 240) {
			yOffs = 240 - loc.getY();
		} else if (loc.getY() > game.actual.getHeight() * 64 - 240) {
			yOffs = (game.actual.getHeight() * 64 - 240) - loc.getY();
		} else {
			yOffs = 0;
		}
	}

	@Override
	public Bitmap getDrawGraphics() {
		if (moving) {
			return playerBM[animphase % 2 + dir * 3 + 1];
		} else {
			return playerBM[dir * 3];
		}
	}

	@Override
	public boolean isPassable() {
		return false;
	}

	@Override
	public boolean tick() {
		animDelay--;
		if (animDelay <= 0) {
			animDelay = 5;
			animphase++;
			animphase %= 2;

		}
		restDelay--;
		if (restDelay <= 0) {
			restDelay = 20;
			if (!running) {
				if (energy < 100) {
					int from = energy;
					energy++;
					if (from < 30 && energy >= 30) {
						resting = false;
					}
				}
			}
		}
		if(damageDelay > 0){
			damageDelay--;
		}
		checkControls();
		checkMap();

		energyBar.setVal(energy);
		return true;
	}
	
	public void interact(){
		List<Entity> entity = game.actual.getEntity(loc.getTile(dir, game.actual));
		for(Entity e : entity){
			if(e instanceof Chest){
				((Chest) e).open(this);
			}
		}
	}
	
	public void tryDamage(){
		if(damageDelay != 0) return;
		
		
		List<Entity> entity = game.actual.getEntity(loc.getTile(dir, game.actual));
		for(Entity e : entity){
			if(e instanceof LivingEntity){
				damageDelay = 30;
				Random r = new Random();
				int dmg = r.nextInt(10) + 10;
				System.out.println(dmg);
				
				boolean damage = ((LivingEntity) e).damage(this, dmg);
				if(damage){
					game.actual.entities.remove(e);
				}
			}
		}
	}
	
	public Inventory getInventory(){
		return inventory;
	}

	
}
